<script setup lang="ts">
import { ref, onMounted, nextTick, onBeforeMount } from "vue";
import DialogForm from "./components/DialogForm.vue";
import { TableProBar } from "@/components/ReTable";
import { GetAccList } from "@/api/joinSignItem";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { FormInstance } from "element-plus";
import { GetListNVForApp } from "@/api/projectWinBid";
import { GetSupplyListNV } from "@/api/purchasing";
import { exportExcel } from "@/api/exportAll";
import { useGlobal } from "@pureadmin/utils";
import { getUserListByRoleCodeNew } from "@/api/user";
import { tableButtons, operationButtons } from "./constant";
import { TableColumOperation } from "@/components/TableColumOperation";

const { $config } = useGlobal<GlobalPropertiesApi>();
const roleCodeStaffListMap = {
  purchasingSpeciaList: $config.RoleCodePurchasingSpecialist
};

defineOptions({
  name: "SubLedger"
});

// vue3 声明原始数据类型的响应式变量，另外如果是对象类型，则使用reactive方法
const tableHeight = ref(0);
const dataList = ref([]);
const totalPage = ref(0);
const loading = ref(true);
const fold = ref(true);
const maxItemNum = ref(1);
const supplyList = ref([]); //申请状态
const formRef = ref<FormInstance>();
const dialogFormRef = ref(null);
const projectList = ref([]);
const staffList = ref({
  purchasingSpeciaList: []
});

const queryForm = ref({
  projectId: "",
  supplyId: "",
  staffId: "",
  keyword: "",
  supplyContractCode: "",
  pageIndex: 1,
  pageSize: 20
});
//序号列
function getIndex(index) {
  const page = queryForm.value.pageIndex;
  const pagesize = queryForm.value.pageSize;
  return (page - 1) * pagesize + index + 1;
}
// 查看表格行
function handleQuery(row) {
  dialogFormRef.value.show(row, "query");
}

//导出
async function handleExport() {
  const fileName = "项目明细台账";
  exportExcel("/api/JoinSignItem/ExportAccList?", fileName, queryForm.value);
}

// current-change 改变时触发
function handleCurrentChange() {
  onSearch();
}

// pageSize 改变时触发
function handleSizeChange() {
  onSearch();
}

// 表格行选中回调
function handleSelectionChange(val) {
  console.log("handleSelectionChange", val);
}

// 搜索获取表格数据
async function onSearch() {
  loading.value = true;
  GetAccList(queryForm.value)
    .then(({ data }) => {
      const _data = data.data || [];
      dataList.value = _data.map(item => {
        item.disabled = {};
        item.title = {};

        return item;
      });

      totalPage.value = data.totalCount;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
      dataList.value = [];
    });
}

function handleOperation(functionName, row?) {
  console.log("functionName", functionName);
  switch (functionName) {
    case "handleExport":
      handleExport();
      break;
    case "handleQuery": //选择采购项
      handleQuery(row);
      break;
  }
}

// 根据部门Id获取员工列表
const getStaffListByDeptId = async (roleCode, staffType) => {
  const { data = {} } = await getUserListByRoleCodeNew({
    roleCode
  });
  staffList.value[staffType] = data || [];
};
function handlepurchasingSpeciachange(val) {
  const staff = staffList.value.purchasingSpeciaList.find(
    item => item.staffId == val
  );
  queryForm.value.staffId = staff.staffId;
  formRef.value.validateField("staffId", () => null);
}

onBeforeMount(() => {
  Object.keys(roleCodeStaffListMap).forEach(item => {
    getStaffListByDeptId(roleCodeStaffListMap[item], item);
  });
});

// //切换“折叠”与“展开”
// function handleFold() {
//   fold.value = !fold.value;
//   setTableHeight();
// }

// 重置查询条件表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  onSearch();
};

//供应商NV;
async function getSupply() {
  const { data } = await GetSupplyListNV();
  supplyList.value = data;
}

getSupply();

// 获取中标项目NV数据
function getProjectList() {
  GetListNVForApp()
    .then(({ data }) => {
      projectList.value = data || [];
    })
    .catch(() => {
      projectList.value = [];
    });
}

getProjectList();

// 设置表格组件高度
const setTableHeight = () => {
  nextTick(() => {
    maxItemNum.value =
      (formRef.value.$el.clientWidth -
        8 -
        32 -
        formRef.value.$el.children[formRef.value.$el.children.length - 1]
          .clientWidth) /
      (formRef.value.$el.children[0].clientWidth + 32);
    maxItemNum.value = Math.floor(maxItemNum.value);
    tableHeight.value =
      window.innerHeight - formRef.value.$el.clientHeight - 230;
  });
};

//mounted周期函数
onMounted(async () => {
  onSearch();
  nextTick(() => {
    setTableHeight();
  });
});

// 窗口尺寸改变事件回调
window.onresize = function () {
  console.log("onresize");
  nextTick(() => {
    setTableHeight();
  });
};
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="queryForm"
      label-width="90px"
      class="bg-bg_color w-100/100 pl-2 pt-4 mb-2"
    >
      <el-form-item
        label="项目名称"
        prop="projectId"
        v-show="maxItemNum >= 1 || !fold"
      >
        <el-select
          v-model="queryForm.projectId"
          placeholder="请选择"
          filterable
          style="width: 100%"
        >
          <el-option label="全部" value="" />
          <el-option
            v-for="item in projectList"
            :key="item.id"
            :label="item.fullName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="供应商名称"
        prop="supplyId"
        v-show="maxItemNum >= 2 || !fold"
      >
        <el-select clearable v-model="queryForm.supplyId" placeholder="请选择"
          ><el-option
            v-for="item in supplyList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        /></el-select>
      </el-form-item>

      <el-form-item
        label="采购负责人"
        prop="staffId"
        v-show="maxItemNum >= 3 || !fold"
      >
        <el-select
          clearable
          v-model="queryForm.staffId"
          placeholder="请选择"
          @change="handlepurchasingSpeciachange"
          ><el-option
            v-for="item in staffList.purchasingSpeciaList"
            :key="item.staffId"
            :label="item.staffName"
            :value="item.staffId"
        /></el-select>
      </el-form-item>

      <!-- <el-form-item
        label="合同编号"
        prop="supplyContractCode"
        v-show="maxItemNum >= 4 || !fold"
      >
        <el-input
          placeholder="请输入"
          v-model="queryForm.supplyContractCode"
          clearable
          @keyup.enter="onSearch"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('search')"
          :loading="loading"
          @click="onSearch"
        >
          查询
        </el-button>
        <el-button :icon="useRenderIcon('refresh')" @click="resetForm(formRef)">
          重置
        </el-button>
        <!-- <el-button type="text" @click="handleFold">
          <span v-show="fold">展开</span>
          <span v-show="!fold">折叠</span>
          <IconifyIconOffline :icon="!fold ? 'arrow-up' : 'arrow-down'" />
        </el-button> -->
      </el-form-item>
    </el-form>

    <TableProBar
      title="项目明细台账"
      :loading="loading"
      :dataList="dataList"
      @refresh="onSearch"
    >
      <template #buttons>
        <el-button
          v-for="(item, index) in tableButtons"
          :key="index"
          :type="item.buttonType"
          :icon="useRenderIcon(item.buttonIcon)"
          @click="handleOperation(item.buttonClick)"
        >
          {{ item.buttonName }}
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <el-table
          border
          :size="size"
          :height="tableHeight"
          :data="dataList"
          highlight-current-row
          @selection-change="handleSelectionChange"
          :default-expand-all="false"
          row-key="path"
        >
          <el-table-column
            v-if="checkList.includes('勾选列')"
            type="selection"
            align="center"
            width="60"
          />
          <el-table-column
            v-if="checkList.includes('序号列')"
            fixed="left"
            type="index"
            :index="getIndex"
            label="序号"
            width="60"
            align="center"
          />
          <el-table-column label="项目设备">
            <el-table-column
              label="项目名称"
              prop="projectFullName"
              min-width="150"
              align="letf"
              show-overflow-tooltip
            />
            <el-table-column
              label="设备名称"
              prop="materialName"
              min-width="150"
              align="letf"
              show-overflow-tooltip
            />
            <el-table-column
              min-width="150"
              label="型号"
              prop="materialSpec"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="单位"
              prop="materialUnit"
              width="100"
              align="center"
              show-overflow-tooltip
            />
            <el-table-column
              label="数量"
              prop="materialQty"
              width="100"
              align="right"
              show-overflow-tooltip
            />
            <el-table-column
              label="单价"
              prop="materialPrice"
              width="100"
              align="right"
              show-overflow-tooltip
            />
            <el-table-column
              label="小计"
              prop="subTotal"
              width="120"
              align="right"
              show-overflow-tooltip
            />
            <el-table-column
              label="供应商名称"
              prop="supplyName"
              width="120"
              align="left"
              show-overflow-tooltip
            />
            <!-- <el-table-column
            label="合计"
            prop="qty"
            width="120"
            align="right"
            show-overflow-tooltip
          /> -->
            <el-table-column
              label="采购负责人"
              prop="handStaffName"
              width="120"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="备注"
              prop="remark"
              min-width="150"
              align="left"
              show-overflow-tooltip
            />
          </el-table-column>
          <el-table-column label="各站点销售">
            <el-table-column
              label="设备名称"
              prop="materialName"
              min-width="150"
              align="letf"
              show-overflow-tooltip
            />

            <el-table-column
              label="单位"
              prop="materialUnit"
              width="100"
              align="center"
              show-overflow-tooltip
            />
            <el-table-column
              label="数量"
              prop="materialQty"
              width="100"
              align="right"
              show-overflow-tooltip
            />
            <el-table-column label="施工图数量">
              <el-table-column
                width="100"
                label="宁化"
                prop="materialQty"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="清流"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="明溪"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="三元"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />

              <el-table-column
                label="大田"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="德化"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="永春"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="安溪东"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="南安北"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="泉州"
                prop="materialQty"
                width="100"
                align="left"
                show-overflow-tooltip
              />
              <el-table-column
                label="合计"
                prop="subTotal"
                width="100"
                align="left"
                show-overflow-tooltip
              />
            </el-table-column>
          </el-table-column>
          <el-table-column label="各站点采购">
            <el-table-column
              width="100"
              label="宁化"
              prop="materialQty"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="清流"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="明溪"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="三元"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />

            <el-table-column
              label="大田"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="德化"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="永春"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="安溪东"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="南安北"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="泉州"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="合计"
              prop="subTotal"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="projectFullName"
              min-width="150"
              align="letf"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="materialName"
              min-width="150"
              align="letf"
              show-overflow-tooltip
            />
            <el-table-column
              min-width="150"
              label="XX"
              prop="materialSpec"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="materialUnit"
              width="100"
              align="center"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="materialQty"
              width="100"
              align="right"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="materialPrice"
              width="100"
              align="right"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="subTotal"
              width="120"
              align="right"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="supplyName"
              width="120"
              align="left"
              show-overflow-tooltip
            />

            <el-table-column
              label="XX"
              prop="handStaffName"
              width="120"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="remark"
              min-width="150"
              align="left"
              show-overflow-tooltip
            />
          </el-table-column>
          <el-table-column label="各站点到货情况">
            <el-table-column
              width="100"
              label="宁化"
              prop="materialQty"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="清流"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="明溪"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="三元"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />

            <el-table-column
              label="大田"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="德化"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="永春"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="安溪东"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="南安北"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />
            <el-table-column
              label="泉州"
              prop="materialQty"
              width="100"
              align="left"
              show-overflow-tooltip
            />

            <el-table-column
              label="XX"
              prop="projectFullName"
              min-width="150"
              align="letf"
              show-overflow-tooltip
            />
            <el-table-column
              label="XX"
              prop="materialName"
              min-width="150"
              align="letf"
              show-overflow-tooltip
            />
            <el-table-column
              min-width="150"
              label="XX"
              prop="materialSpec"
              align="left"
              show-overflow-tooltip
            />
          </el-table-column>
          <TableColumOperation
            v-if="operationButtons().length > 0"
            :size="size"
            :operationButtons="operationButtons()"
            @operation="handleOperation"
          />

          <template #empty>
            <el-empty description="暂无数据" />
          </template>
        </el-table>
        <el-pagination
          class="flex justify-end mt-4"
          :small="size === 'small' ? true : false"
          v-model:current-page="queryForm.pageIndex"
          v-model:page-size="queryForm.pageSize"
          :page-sizes="[10, 20, 30, 50, 100, 200]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </template>
    </TableProBar>
    <DialogForm @search="onSearch" ref="dialogFormRef" />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
